package com.irdstudio.efp.nls.service.impl.sx;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.exception.HangException;
import com.irdstudio.efp.console.service.facade.PrdInfoService;
import com.irdstudio.efp.console.service.vo.PrdInfoVO;
import com.irdstudio.efp.esb.common.constant.MsLoanConstant;
import com.irdstudio.efp.esb.service.bo.req.ZX11001QueryReports;
import com.irdstudio.efp.esb.service.bo.resp.zx.ZX11001ResponseReport;
import com.irdstudio.efp.esb.service.facade.zx.ZXService;
import com.irdstudio.efp.esb.service.vo.zx.ZXVO;
import com.irdstudio.efp.nls.service.vo.queue.NlsProcessBizVO;
import com.irdstudio.efp.nls.service.vo.sx.NlsCreditInfoVO;

/**
 * 黑名单判断服务
  * @author heyingfeng
  * @since 2019年3月9日 上午11:31:52
  * @version 1.0
 */
@Service("creditBlackService")
public class CreditBlackServiceImpl extends CreditSoltServiceImpl {

	private static final Logger log = LoggerFactory.getLogger(CreditBlackServiceImpl.class);
	private static final String blackRule = "BdAccessRule010";
	
	
	@Override
	public void doExecute(NlsProcessBizVO nlsProcessBizVo,NlsCreditInfoVO nlsCreditInfoVO) throws Exception {
		String applySeq = nlsProcessBizVo.getApplySeq();
		try {
			log.info("开始进行黑名单规则产品,流水号:" + applySeq);
			Objects.requireNonNull(nlsCreditInfoVO);
			
			//查询产品
			String prdId = Objects.requireNonNull(nlsCreditInfoVO.getPrdId());
			PrdInfoVO prdInfoVo = new PrdInfoVO();
			prdInfoVo.setPrdId(prdId);
			prdInfoVo = prdInfoService.queryByPk(prdInfoVo);
			
			//黑名单核查
			int blackListNum = 0;
			String pbocCertTp = "0";
			ZX11001QueryReports zx11001QueryReports = new ZX11001QueryReports.Builder().withName(nlsCreditInfoVO.getCusName()).withCertNo(nlsCreditInfoVO.getCertCode()).withCertType(pbocCertTp) // 征信的证件类型-身份证（目前只支持身份证）
					.build();
			try {
				ZXVO zxvo = zx11001Service.service(zx11001QueryReports,prdInfoVo.getZxOpId());
				log.info("黑名单规则参数准备,流水号：" + applySeq + ",查询黑名单接口完成");
				@SuppressWarnings("unchecked")
				ZX11001ResponseReport content = ((List<ZX11001ResponseReport>) zxvo.getResp()).get(0);
				Objects.requireNonNull(content);
				Supplier<Integer> zero = () -> Integer.valueOf(0);
				// 黑名单命中次数
				blackListNum = Stream.of(content.getBankPushBlklistArry(), content.getHghCrtExecPrsnLstArry(), content.getHghCrtLsCrdblPrsnLstArry(), content.getOwnFraudBlklistArry(),
						content.getOwnSzRskIndBlklistArry(), content.getP2PLsCrdblPrsnLstArry(), content.getOwnRskCardIndBlklistArry(), content.getAbnTxBlklistArry(),
						content.getLoanRskIndBlklistArry(), content.getIndvBlklistInfArry(), content.getSmhldBlkshlistInfArry(), content.getPBCPblcInfBadBlklistArry(),
						content.getConsmFinBsnCustBlklistArry()).map(Optional::ofNullable).map(i -> i.map(List::size).orElseGet(zero)).reduce(0, Integer::sum);
			} catch (Exception e) {
				log.error(e.getMessage());
				nlsCreditInfoVO.setBdReasonCode(MsLoanConstant.ReasonCodeMap.get(blackRule));
				nlsCreditInfoVO.setBdReasonMsg(MsLoanConstant.ReasonMsgMap.get(blackRule));
				throw new HangException("调用黑名单异常", e);
			}
			if(blackListNum>0) {
				/**设置返回开放平台错误信息**/
				nlsCreditInfoVO.setBdReasonCode(MsLoanConstant.ReasonCodeMap.get(blackRule));
				nlsCreditInfoVO.setBdReasonMsg(MsLoanConstant.ReasonMsgMap.get(blackRule));
				nlsProcessBizVo.setRefuseMsg("命中黑名单次数："+blackListNum);
				throw new BizException("命中黑名单次数："+blackListNum);
			}
		} catch (Exception e) {
			log.error(Arrays.toString(e.getStackTrace()));
			nlsProcessBizVo.setExceptionFlag(true);
			nlsProcessBizVo.setExceptionMsg(e);
		}
	}
	
	@Autowired
	@Qualifier("zx11001Service")
	private ZXService<ZX11001QueryReports> zx11001Service;
	@Autowired
	@Qualifier("prdInfoService")
	private PrdInfoService prdInfoService;

}